// 文件上传
import {
  BASE_URL
} from '../constant/index'

export const uploadFileAsync = async (filePath) => {
  return new Promise((resolv, reject) => {
    wx.uploadFile({
      filePath,
      name: 'files',
      url: BASE_URL + '/api/upload',
      async success(res) {
        const result = JSON.parse(res.data)
        console.log(result)
        if (result.code == 200) {
          resolv(result.data[0])
        } else {
          reject()
        }
      },
      fail(err) {
        console.log(err)
        reject()
      }
    })
  })
}

// 获取时间列表
export const generateClassifyList = () => {
  const classifyList = [];
  const today = new Date();
  const weekDays = ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六'];

  for (let i = 0; i <= 15; i++) { // 0代表今天，总共16天
    const date = new Date(today);
    date.setDate(today.getDate() + i);

    const month = (date.getMonth() + 1).toString().padStart(2, '0'); // 月
    const day = date.getDate().toString().padStart(2, '0'); // 日
    const year = date.getFullYear(); // 年

    const dateStr = `${month}月${day}号`; // 04月28号
    const fullDateStr = `${year}-${month}-${day}`; // 2025-04-28

    let desc = '';
    if (i === 0) {
      desc = '今天';
    } else if (i === 1) {
      desc = '明天';
    } else if (i === 2) {
      desc = '后天';
    } else {
      desc = weekDays[date.getDay()]; // 星期几
    }

    classifyList.push({ 
      date: dateStr, 
      desc: desc, 
      fullDate: fullDateStr 
    });
  } 

  return classifyList;
}